<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class UserChannel extends \app\BaseModel
{
    protected $name = "massage_user_channel_bind";
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function getChannelId($user_id)
    {
        $user_model = new User();
        $user = $user_model->dataInfo(["id" => $user_id]);
        if(empty($user)) {
            return [];
        }
        $openid = $user["web_openid"];
        $where = ["user_id" => $user_id, "open_id" => $openid];
        if(!empty($user["unionid"])) {
            $where["unionid"] = $user["unionid"];
        }
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int) $user["uniacid"], ["channelforeverbind"]);
        $channel_bind_forever = $auth["channelforeverbind"];
        if($channel_bind_forever) {
            $data = $this->where(function ($query) use ($where){
                $query->whereOr($where);
            })->find();
        } else {
            $data = $this->where(function ($query) use ($where) {
                $query->whereOr($where);
            })->where("over_time", ">", time())->find();
        }
        if(empty($data)) {
            return [];
        }
        $data = $data->toArray();
        if(!empty($data["channel_qr_id"])) {
            $qr_model = new ChannelQr();
            $qr = $qr_model->dataInfo(["id" => $data["channel_qr_id"]]);
            if(!$channel_bind_forever) {
                if(empty($qr) || $qr["status"] != 1) {
                    return [];
                }
                $data["channel_id"] = $qr["channel_id"];
            } else {
                if($qr["status"] != 1) {
                    $data["channel_qr_id"] = 0;
                }
                if(!empty($qr["channel_id"])) {
                    $data["channel_id"] = $qr["channel_id"];
                } else {
                    $data["channel_qr_id"] = 0;
                }
            }
        }
        if(!empty($data["channel_id"])) {
            $channel_model = new ChannelList();
            $channel = $channel_model->dataInfo(["id" => $data["channel_id"], "status" => 2]);
            if(empty($channel)) {
                if(!empty($data["channel_qr_id"])) {
                    $data["channel_id"] = 0;
                } else {
                    $data = [];
                }
            }
        }
        return $data;
    }
}

?>